package main

import (
	"fmt"
	"sync"
	"time"
)

func job(index int) int {
	time.Sleep(time.Millisecond * 500)
	return index
}

func main() {
	start := time.Now()
	num := 5
	resultChan := make(chan int)
	// for i := 0; i < num; i++ {
	// 	fmt.Println(job(i))
	// }
	wg := sync.WaitGroup{}
	for i := 0; i < num; i++ {
		wg.Add(1)
		go func(param int) {
			defer wg.Done()
			// fmt.Println(job(param))
			resultChan <- job(param)
		}(i)
	}
	go func() {
		defer close(resultChan)
		wg.Wait()
	}()
	// wg.Wait()
	// count := 0
	for item := range resultChan {
		fmt.Println(item)
		// count++
		// if count == num {
		// 	close(resultChan)
		// }
	}
	end := time.Since(start)
	fmt.Println("cost time:", end.String())
}
